package com.c1202m.dao;

import codeblue.project.MD5Encode;
import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.QuanTri;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;

public class QuanTriDAO {

    public static boolean hienToanBoQT = false;

    public static Vector layToanBoDoiTuong() {
        Vector vt = new Vector();
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query1 = session.createQuery("from QuanTri where trangThai = ?");
            query1.setString(0, "TRUE");
            Query query2 = session.createQuery("from QuanTri");
            List list = query1.list();
            if (hienToanBoQT) {
                list = query2.list();
            }
            for (Object obj : list) {
                QuanTri admin = (QuanTri) obj;
                Vector temp = new Vector();
                temp.add(admin.getMaQtri());
                temp.add(admin.getEmail());
                temp.add(admin.getHoTen());
                temp.add(admin.getGioiTinh());
                temp.add(admin.getNgaySinh());
                temp.add(admin.getDiaChi());
                temp.add(admin.getDienThoai());
                temp.add(admin.getTrangThai());
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static QuanTri layMotDoiTuong(int id) {
        QuanTri quanTri = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            quanTri = (QuanTri) session.get(QuanTri.class, id);
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return quanTri;
    }

    public static boolean them(String email, String hoTen, String gioiTinh, Date ngaySinh, String dienThoai, String diaChi, boolean trangThai) {
        if (!kiemEmailQuanTri(email)) {
            new CustomMessageDialog(null, true, "Warning!", "Email đã tồn tại", CustomMessageDialog.MESSAGE);
            return false;
        }
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            QuanTri admin = new QuanTri();
            admin.setEmail(email);
            admin.setMatKhau(MD5Encode.encode("123456"));
            admin.setHoTen(hoTen);
            admin.setGioiTinh(gioiTinh);
            admin.setNgaySinh(ngaySinh);
            admin.setDienThoai(dienThoai);
            admin.setDiaChi(diaChi);
            admin.setTrangThai(trangThai);
            session.save(admin);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean sua(int id, String email, String hoTen, String gioiTinh, Date ngaySinh, String dienThoai, String diaChi, boolean trangThai) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            QuanTri admin = (QuanTri) session.get(QuanTri.class, id);
            admin.setEmail(email);
            admin.setHoTen(hoTen);
            admin.setGioiTinh(gioiTinh);
            admin.setNgaySinh(ngaySinh);
            admin.setDienThoai(dienThoai);
            admin.setDiaChi(diaChi);
            admin.setTrangThai(trangThai);
            session.update(admin);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean xoa(int id) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            QuanTri admin = (QuanTri) session.get(QuanTri.class, id);
            session.delete(admin);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean layLaiMatKhau(int id) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            QuanTri quanTri = (QuanTri) session.get(QuanTri.class, id);
            quanTri.setMatKhau(MD5Encode.encode("123456"));
            session.update(quanTri);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean kiemEmailQuanTri(String email) {
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from QuanTri a where a.email = ?");
            query.setString(0, email);
            list = query.list();
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return list.isEmpty() ? true : false;
    }
}